//
//  LLPickTextField.m
//  JoySpell
//
//  Created by linl on 2018/3/26.
//  Copyright © 2018年 xkdao. All rights reserved.
//

#import "M1PickerTF.h"

@interface M1PickerTF () <UITextFieldDelegate>


@property (nonatomic) UIView *m1_Accessory;

@property (nonatomic) UIButton *m1_buttonWindow;


@property (nonatomic) NSMutableArray *m1_Indexes;

@end

@implementation M1PickerTF

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

#pragma mark - lazy load

- (void)setM1_type:(EnumPickField)type
{
    _m1_type = type;
    
    self.inputView = nil;
    
    self.inputAccessoryView = self.m1_Accessory;
    
    [[self valueForKey:@"textInputTraits"] setValue:[UIColor blueColor] forKey:@"insertionPointColor"];
    
}



- (UIView *)m1_Accessory
{
    if (!_m1_Accessory) {
        _m1_Accessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
        _m1_Accessory.backgroundColor = [UIColor whiteColor];
        
        UIButton *m1_cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
        m1_cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [m1_cancelButton setTitleColor:[UIColor m1_colorHex:0x666666] forState:0];
        [m1_cancelButton setTitle:@"取消" forState:0];
        [m1_cancelButton addTarget:self action:@selector(m1_Cancel) forControlEvents:UIControlEventTouchUpInside];
        [_m1_Accessory addSubview:m1_cancelButton];
        
        UIButton *m1_okButton = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 80, 0, 80, 44)];
        m1_okButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [m1_okButton setTitleColor:[UIColor m1_colorHex:0x666666] forState:0];
        [m1_okButton setTitle:@"确定" forState:0];
        [m1_okButton addTarget:self action:@selector(m1_doAccessoryOk) forControlEvents:UIControlEventTouchUpInside];
        [_m1_Accessory addSubview:m1_okButton];
    }
    return _m1_Accessory;
}

- (UIButton *)m1_buttonWindow
{
    if (!_m1_buttonWindow) {
        _m1_buttonWindow = [UIButton buttonWithType:UIButtonTypeCustom];
        _m1_buttonWindow.frame = self.window.frame;
        _m1_buttonWindow.backgroundColor = [UIColor colorWithRed:1/255.0 green:1/255.0 blue:1/255.0 alpha:1];
        [_m1_buttonWindow addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.window addSubview:_m1_buttonWindow];
    }
    return _m1_buttonWindow;
}


- (NSMutableArray *)m1_Indexes
{
    if (!_m1_Indexes) {
        _m1_Indexes = [[NSMutableArray alloc] init];
    }
    return _m1_Indexes;
}

#pragma mark - private method
- (void)initSubviews
{

}




- (void)m1_addViewToWindow
{
    [self.window addSubview:self.m1_buttonWindow];
    self.m1_buttonWindow.alpha = 0;
    [UIView animateWithDuration:0.25f animations:^{
        self.m1_buttonWindow.alpha = 0.5;
    }];
    
}

- (void)m1_removeFromWindow
{
    [UIView animateWithDuration:0.25f animations:^{
        self.m1_buttonWindow.alpha = 0;
    } completion:^(BOOL finished) {
        [self.m1_buttonWindow removeFromSuperview];
    }];
}

#pragma mark - event
- (void)m1_Cancel
{
    [self endEditing:YES];
    [self m1_removeFromWindow];
}
- (void)m1_doAccessoryOk
{
    [self endEditing:YES];
    [self m1_removeFromWindow];
    
}

- (void)m1_ClickBackGround
{
   
}

- (void)buttonPressed
{
    [self endEditing:YES];

    [self m1_removeFromWindow];
}

#pragma mark - UITextFieldDelegate
- (CGRect)editingRectForBounds:(CGRect)bounds
{
    CGRect originalRect = [super editingRectForBounds:bounds];
    
    CGRect rect = CGRectMake(originalRect.origin.x + self.m1_edgeInsets.left, originalRect.origin.y + self.m1_edgeInsets.top, originalRect.size.width - self.m1_edgeInsets.left - self.m1_edgeInsets.right, originalRect.size.height - self.m1_edgeInsets.top - self.m1_edgeInsets.bottom);
    
    return rect;
}

- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect originalRect = [super textRectForBounds:bounds];
    
    CGRect rect = CGRectMake(originalRect.origin.x + self.m1_edgeInsets.left, originalRect.origin.y + self.m1_edgeInsets.top, originalRect.size.width - self.m1_edgeInsets.left - self.m1_edgeInsets.right, originalRect.size.height - self.m1_edgeInsets.top - self.m1_edgeInsets.bottom);
    
    return rect;
}

@end
